#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Muto Controller Node Launch File

Launches the Muto robot controller node with FastAPI HTTP interface
"""

from launch import LaunchDescription
from launch_ros.actions import Node
from launch.actions import DeclareLaunchArgument, LogInfo
from launch.substitutions import LaunchConfiguration


def generate_launch_description():
    """生成launch描述"""
    
    # 声明launch参数
    server_host_arg = DeclareLaunchArgument(
        'server_host',
        default_value='0.0.0.0',
        description='FastAPI server host address'
    )
    
    server_port_arg = DeclareLaunchArgument(
        'server_port',
        default_value='8080',
        description='FastAPI server port'
    )
    
    log_level_arg = DeclareLaunchArgument(
        'log_level',
        default_value='info',
        description='ROS2 log level (debug, info, warn, error, fatal)'
    )
    
    # 创建Muto控制节点
    muto_controller_node = Node(
        package='muto_ros2_controller',
        executable='muto_controller_node',
        name='muto_controller_node',
        output='screen',
        respawn=True,
        respawn_delay=2.0,
        parameters=[
            {
                'server_host': LaunchConfiguration('server_host'),
                'server_port': LaunchConfiguration('server_port'),
            }
        ],
        arguments=['--ros-args', '--log-level', LaunchConfiguration('log_level')],
        emulate_tty=True,
    )
    
    # 启动信息
    launch_info = LogInfo(
        msg=[
            'Starting Muto Robot Controller Node...\n',
            'FastAPI Server will be available at: http://',
            LaunchConfiguration('server_host'),
            ':',
            LaunchConfiguration('server_port'),
            '\n',
            'Health check endpoint: http://',
            LaunchConfiguration('server_host'),
            ':',
            LaunchConfiguration('server_port'),
            '/health\n',
            'API documentation: http://',
            LaunchConfiguration('server_host'),
            ':',
            LaunchConfiguration('server_port'),
            '/docs'
        ]
    )
    
    return LaunchDescription([
        # Launch参数
        server_host_arg,
        server_port_arg,
        log_level_arg,
        
        # 启动信息
        launch_info,
        
        # 节点
        muto_controller_node,
    ])
